package com.yungee.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yungee.base.entity.JsonHistory;
import com.yungee.logic.JsonLogic;
import com.yungee.vo.resp.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Properties;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto-generate
 * @since 2024-05-11
 */
@RestController
@RequestMapping("/jsonHistory")
@Slf4j
public class JsonHistoryController {
    @Autowired
    private JsonLogic jsonLogic;

    @PostMapping("saveJson")
    public AjaxResult saveJson(@RequestBody JSONObject content){
        jsonLogic.saveJson(content);
        return AjaxResult.success("ok");
    }
    @GetMapping("getLatestChangeJson")
    public AjaxResult getLatestChangeJson(){
        return AjaxResult.success(jsonLogic.getLatestChangeJson());
    }
    @GetMapping("query")
    public AjaxResult query(@RequestParam int pageNum,@RequestParam int pageSize){
        Page<JsonHistory> page = jsonLogic.page(pageNum, pageSize);
        return AjaxResult.success(page);
    }

    @GetMapping("nacosTest")
    public AjaxResult nacosTest(String serviceName) throws NacosException {
        Properties properties = new Properties();
        properties.setProperty("serverAddr", "server2:8848");
        properties.setProperty("namespace", "public");
        NamingService naming = NamingFactory.createNamingService(properties);
        List<Instance> allInstances = naming.getAllInstances("providers:com.chujie.api.facade.UserFacade:1.0.0:", "hongyu-prod");
        System.out.println(JSONArray.toJSONString(allInstances));
        return AjaxResult.success("ok");
    }

    public static void main(String[] args) throws NacosException {
        Properties properties = new Properties();
        properties.setProperty("serverAddr", "server2:8848");
        properties.setProperty("namespace", "public");
        NamingService naming = NamingFactory.createNamingService(properties);
        List<Instance> allInstances = naming.getAllInstances("providers:com.chujie.api.facade.UserFacade:1.0.0:", "hongyu-prod");
        System.out.println(JSONArray.toJSONString(allInstances));
    }
}
